export function findText(text, obj) {
    console.log('text', text, obj);
    if(!text) {
        $('#errmsg').text('请输入中文后查找');
        return
    }
    const resList = findObj(text, obj);
    console.log('findObj(text,obj);', resList);
    $('#findResList').html('');
    if (resList.length === 0) {
        $('#errmsg').text('未找到匹配项');
    $('#findResList').html('未找到匹配项');

        return;
    }
    resList.map((item) => {
        const html = `<div class="row">
        <div class="value">${item.text}</div>
            <div class="key">${item.key}</div>
            <button id="copy-${item.key}">复制</button>
            <div class="tips success"></div>
        </div>`;
        $('#findResList').append(html);
    });
}
$('#findRes').on('click', (e) => {
    console.log('findRes', e);
    const target = e.target;
    console.log('target', target);
    if (target.id.indexOf('copy') !== -1) {
        console.log('准备复制');
        console.log('key', $(target).parent().find('.key').text());
        const key = $(target).parent().find('.key').text();
        utools.copyText(`$t('${key}')`);
        $(target).parent().find('.tips').text('复制成功');
        setTimeout(() => {
         $(target).parent().find('.tips').text('');
            
        }, 1000);
    }
});
// 查找自身第一层属性
function findObj(text, obj, lastKey = '') {
    let resList = [];
    for (const key in obj) {
        if (Object.hasOwnProperty.call(obj, key)) {
            const element = obj[key];
            if (typeof element == 'string') {
                // 对比字符串
                if (element.indexOf(text) > -1) {
                    console.log('findObj', key, element);
                    resList.push({
                        key: lastKey + key,
                        text: element,
                        lastKey: lastKey,
                    });
                }
            } else if (typeof element == 'object') {
                // 递归
                const res = findObj(text, element, lastKey + key + '.');
                if (res) {
                    resList = resList.concat(res);
                }
            }
        }
    }
    return resList;
}

